<?php

namespace app\api\controller\v1;

use addons\litestore\model\Litestoregoodsspec;
use addons\litestore\model\Wxlitestoregoods;
use app\admin\model\litestore\Litestoregoods;
use app\admin\model\litestore\Litestorenews;
use app\admin\model\Profitorder;
use app\admin\model\Profitorderundo;
use app\admin\model\Profitvip;
use app\api\service\ProfitService;
use app\common\controller\Api;

/**
 */
class Unprofit extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];



    /**
     * 商品销售利润
     */
    public function myProductProfit(){


        $page = $this->request->param('page');
        $date = $this->request->param('date');

        if(!$page) $page =1;

        $user = $this->auth->getUser();
        $where = "";

        if($date){
            error_log("======date======".$date);
            $startToday = strtotime($date . " 00:00:00");
            $endToday = strtotime($date . " 23:59:59");
            $where .= " and o.createtime >= ".$startToday." and o.createtime <= ".$endToday;
        }

        $profitOrderModel  = new Profitorderundo();
        $limit = 10;
        $offset = ($page - 1) * $limit;



        $sql = "select username, o.id,og.id, uid,orderId,amount,ratio,o.createtime,type,goods_name,order_id,SUM(total_price) as price,count(og.id) as total from u_profit_order_undo o left join u_user u on u.id = o.uid left join u_litestore_order_goods og  on o.orderId = og.order_id where o.uid = ".$user['id'].$where." GROUP BY orderId limit ".$offset." ,10";
        error_log("===sql=".$sql);
        $list = $profitOrderModel->query($sql);
        foreach ($list as $key=>$val){
            $list[$key]['createtime'] = date('Y-m-d',$val['createtime']);
            if($val['type'] == 'self'){
                $list[$key]['typeText'] = '零售差价';
            }else if($val['type' == 'team']){
                $list[$key]['typeText'] = '下单费';
            }
        }
        $this->success('',$list);
    }




}
